/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef GLOBALDATA_H
#define GLOBALDATA_H

// qt
#include <QMutex>
#include <QVariantMap>
#include <QHostAddress>

// gparams
#include "gparams.h"

// analyzer
#include "analyzer.h"

/// Collection of global data shared between server threads. Follows a 'monitor' design pattern,
/// which means it has all data uarded by mutex.
class GlobalData
{
public:

	static GlobalData* instance();	///< Provides instance pointer
	
	// params 
	
	/// Sets params icoming from client
	void setParams( const GParams& params );
	/// Provides params for main loop
	GParams getParams();
	/// Checks if params new
	bool paramsNew() const { return _paramsNew; }
	
	// masks
	void setNewMasks( bool newMasks) { _masksNew = newMasks; }
	bool masksNew() const { return _masksNew; }
	
	// connection count 
	
	void incConnections();	///< Increase number of active connections
	void decConnections();	///< Decrease number of active connections
	/// Returns number of connections
	int connectionsNumber() { return _connectionsNum; }
	
	// udp sending address
	/// Sets address incoming from client
	void setUpdAddress( const QHostAddress& addr, quint16 port );
	/// Gets udp port
	quint16 getUdpPort() { return _udpHostPort; } // thos does not needs locking
	/// Gets udp address
	QHostAddress getUdpAddress();
	
	/// Checks if address has changed
	bool addressNew() const { return _addressNew; }
	/// Announces new address
	void announceNewAddress() { _addressNew = true; }
	/// Acknowledes new address
	void acknowledgeNewAddress() { _addressNew = false; }
	
private:

	GlobalData();					///< Constructor. Initializes some members

	QMutex				_mutex;		///< General mutex
	
	static GlobalData*	_pInstance;	///< Global instance
	
	// params
	bool				_paramsNew;
	GParams  			_params;
	
	// masks (stored in file)
	bool				_masksNew;
	
	// connection count
	int 				_connectionsNum;	///< Numebr of currently connected clients
	
	// upd sendin address
	QHostAddress		_udpHostAddress;
	quint16				_udpHostPort;
	bool				_addressNew;
};

#endif // GLOBALDATA_H

// eif 

